﻿using System;
using System.Configuration;
using System.Data;
using System.Data.Objects;
using Diploma.Models.IRepositories;
using Diploma.Models.Repositories;

namespace Diploma.Models
{
    public class DataManager
    {
        private static volatile DataManager instance;
        private static readonly object syncRoot = new Object();

        private readonly DiplomaEntities dataContext;
        private readonly CommonEntities commonEntities;

        #region Repositories Diploma

        private ICheckPointsRepository checkPointsRepository;
        private ICollaborationRoomRepository collaborationRoomRepository;
        private IGlobalTasksCompletionsRepository globalTasksCompletionsRepository;
        private IGlobalTasksRepository globalTasksRepository;
        private ILecturersLoadingsRepository lecturersLoadingsRepository;
        private ILecturersRepository lecturersRepository;
        private IMessagesRepository messagesRepository;
        private IOrderDiplomRepository orderDiplomRepository;
        private IOrderPracticeRepository orderPracticeRepository;
        private IOrderStatusRepository orderStatusRepository;
        private IPlaningTypesRepository planingTypesRepository;
        private IPlaningsRepository planingsRepository;
        private IPublicMessagesRepository publicMessagesRepository;
        private IQualificationLevelsRepository qualificationLevelsRepository;
        private IRolesRepository rolesRepository;
        private IStudentsGroupsRepository studentsGroupsRepository;
        private IStudentsRepository studentsRepository;
        private ISubOrderPracticeRepository subOrderPracticeRepository;
        private ITasksRepository tasksRepository;
        private IUsersRepository usersRepository;
        private INormControlDocsRepository normControlDocsRepository;
        private ICompaniesRepository companiesRepository;
        private IConsultantOfPartRepository consultantOfPartRepository;

        #endregion

        #region Repositories Diploma Common

        private ILoggerRepository loggerRepository;

        #endregion

        private DataManager()
        {
            dataContext = new DiplomaEntities();
            commonEntities = new CommonEntities();
        }

        public static DataManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new DataManager();
                    }
                }

                return instance;
            }
        }

        public IRolesRepository RolesRepository
        {
            get
            {
                if (rolesRepository == null)
                    rolesRepository = new RolesRepository(dataContext);
                return rolesRepository;
            }
        }

        public IStudentsRepository StudentsRepository
        {
            get
            {
                if (studentsRepository == null)
                    studentsRepository = new StudentsRepository(dataContext);
                return studentsRepository;
            }
        }

        public ILecturersRepository LecturersRepository
        {
            get
            {
                if (lecturersRepository == null)
                    lecturersRepository = new LecturersRepository(dataContext);
                return lecturersRepository;
            }
        }

        public IUsersRepository UsersRepository
        {
            get
            {
                if (usersRepository == null)
                    usersRepository = new UsersRepository(dataContext);
                return usersRepository;
            }
        }

        public ICollaborationRoomRepository CollaborationRoomRepository
        {
            get
            {
                if (collaborationRoomRepository == null)
                    collaborationRoomRepository = new CollaborationRoomRepository(dataContext);
                return collaborationRoomRepository;
            }
        }

        public IMessagesRepository MessagesRepository
        {
            get
            {
                if (messagesRepository == null)
                    messagesRepository = new MessagesRepository(dataContext);
                return messagesRepository;
            }
        }

        public IPublicMessagesRepository PublicMessagesRepository
        {
            get
            {
                if (publicMessagesRepository == null)
                    publicMessagesRepository = new PublicMessagesRepository(dataContext);
                return publicMessagesRepository;
            }
        }

        public IQualificationLevelsRepository QualificationLevelsRepository
        {
            get
            {
                if (qualificationLevelsRepository == null)
                    qualificationLevelsRepository = new QualificationLevelsRepository(dataContext);
                return qualificationLevelsRepository;
            }
        }

        public IStudentsGroupsRepository StudentsGroupsRepository
        {
            get
            {
                if (studentsGroupsRepository == null)
                    studentsGroupsRepository = new StudentsGroupsRepository(dataContext);
                return studentsGroupsRepository;
            }
        }

        public IGlobalTasksRepository GlobalTasksRepository
        {
            get
            {
                if (globalTasksRepository == null)
                    globalTasksRepository = new GlobalTasksRepository(dataContext);
                return globalTasksRepository;
            }
        }

        public IPlaningsRepository PlaningsRepository
        {
            get
            {
                if (planingsRepository == null)
                    planingsRepository = new PlaningsRepository(dataContext);
                return planingsRepository;
            }
        }

        public IPlaningTypesRepository PlaningTypesRepository
        {
            get
            {
                if (planingTypesRepository == null)
                    planingTypesRepository = new PlaningTypesRepository(dataContext);
                return planingTypesRepository;
            }
        }

        public ICheckPointsRepository CheckPointsRepository
        {
            get
            {
                if (checkPointsRepository == null)
                    checkPointsRepository = new CheckPointsRepository(dataContext);
                return checkPointsRepository;
            }
        }

        public ITasksRepository TasksRepository
        {
            get
            {
                if (tasksRepository == null)
                    tasksRepository = new TasksRepository(dataContext);
                return tasksRepository;
            }
        }

        public IOrderPracticeRepository OrderPracticeRepository
        {
            get { return orderPracticeRepository ?? (orderPracticeRepository = new OrderPracticeRepository(dataContext)); }
        }

        public IOrderDiplomRepository OrderDiplomRepository
        {
            get { return orderDiplomRepository ?? (orderDiplomRepository = new OrderDiplomRepository(dataContext)); }
        }

        public ILecturersLoadingsRepository LecturersLoadingsRepository
        {
            get
            {
                if (lecturersLoadingsRepository == null)
                    lecturersLoadingsRepository = new LecturersLoadingsRepository(dataContext);
                return lecturersLoadingsRepository;
            }
        }

        public IGlobalTasksCompletionsRepository GlobalTasksCompletionsRepository
        {
            get
            {
                if (globalTasksCompletionsRepository == null)
                    globalTasksCompletionsRepository = new GlobalTasksCompletionsRepository(dataContext);
                return globalTasksCompletionsRepository;
            }
        }

        public INormControlDocsRepository NormControlDocsRepository 
        {
            get 
            {
                if (normControlDocsRepository == null)
                    normControlDocsRepository = new NormControlDocsRepository(dataContext);
                return normControlDocsRepository;
            }
        }

        public IOrderStatusRepository OrderStatusRepository
        {
            get { return orderStatusRepository ?? (orderStatusRepository = new OrderStatusRepository(dataContext)); }
        }

        public ISubOrderPracticeRepository SubOrderPracticeRepository
        {
            get
            {
                return subOrderPracticeRepository ??
                       (subOrderPracticeRepository = new SubOrderPracticeRepository(dataContext));
            }
        }

        public ILoggerRepository LoggerRepository
        {
            get {
                    return loggerRepository ??
                            (loggerRepository = new LoggerRepository(commonEntities));
            }
        }

        public ICompaniesRepository CompaniesRepository
        {
            get
            {
                return companiesRepository ?? (companiesRepository = new CompaniesRepository(dataContext));
            }
        }

        public IConsultantOfPartRepository ConsultantOfPartRepository
        {
            get
            {
                return consultantOfPartRepository ?? (consultantOfPartRepository = new ConsultantOfPartRepository(dataContext));
            }
        }

        public void RefreshData()
        {
            dataContext.Refresh(RefreshMode.StoreWins,
                                 dataContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified));

            commonEntities.Refresh(RefreshMode.StoreWins,
                                 dataContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified));
        }
    }
}